<?php
/**
 * 货品列表管理控制器
 */
class GoodsListController extends AuthController{
    private $model;
    public function __init(){
        parent::__init();
        $this->model = K('GoodsList');
    }
    /**
     * 货品列表
     */
    public function index(){ 
        $gid = Q('get.gid',0,'intval');
        // 关联条件
        $where = "ag.goods_gid='{$gid}' AND class=1";
        // 类型属性表与商品属性表关联
        $join = "__type_attr__ ta JOIN __goods_attr__ ag ON ta.taid=ag.type_attr_taid";
        // 商品规格数组
        $attr_data = M()->join($join)->order('taid,gaid')->where($where)->all();
        
        // 组合颜色、尺寸的数组
        foreach ($attr_data as $k => $v) {
            $g_spec_data[$v['taname']][] = $v;
        }
        $this->assign('g_spec_data',$g_spec_data);
        // p($g_spec_data);

        //货品列表数组
        $gl_data = $this->model->where("goods_gid='{$gid}'")->order('glid')->all();
        // p($gl_data);
        foreach ($gl_data as $key => $value) { 
            $gl_data[$key]['spec'] = K('GoodsAttr')->where("gaid IN ({$value['combline']})")->getField("gaid,gavalue",true);
        }
        // p($gl_data);
        $this->assign('gl_data',$gl_data);
        $this->display();
    } 
    /**
     *  添加货品列表
     */
    public function add_goods_list(){
        if (IS_POST) {
            // p(Q('post.'));
            $goods_gid = Q('post.goods_gid'); 
            if (!$this->model->add_goods_list($goods_gid)) $this->error($this->model->error);
            $this->success('添加成功',U('index',array('gid'=>$goods_gid)),'0.5');
        }
    }
    /**
     * 异步检测货品是否存在
     */
    public function check(){
        // p(implode(',', Q('post.spec')));die;
        $where = array('goods_gid'=>intval(Q('get.gid')),'combline'=>implode(',', Q('get.spec'))); 
        // 如果选择的规格,在goods_list中没有，即没有统计时为0，统计到了为1
        $data = M('goods_list')->where($where)->count() ? 1 : 0;
        // p($data);
        $this->ajax($data);   
    } 
    /**
     *  编辑货品列表
     */
    public function edit_goods_list(){
        if (IS_POST) { 
            // p(Q('post.'));die;
            $glid = Q('post.glid');
            $goods_gid = Q('post.goods_gid'); 
            if (!$this->model->edit_goods_list($goods_gid,$glid)) $this->error($this->model->error);
            $this->success('编辑成功',U('index',array('glid'=>$glid,'gid'=>$goods_gid)),'0.5');
        }
        $glid = Q('get.glid',0,'intval');
        $gid = Q('get.gid',0,'intval'); 
        // 商品规格 
        $where = "ag.goods_gid='{$gid}' AND class=1"; 
        $join = "__type_attr__ ta JOIN __goods_attr__ ag ON ta.taid=ag.type_attr_taid";
        $attr_data = M()->join($join)->where($where)->all();
        // p($attr_data);
        // 组合颜色、尺寸的数组
        foreach ($attr_data as $k => $v) {
            $g_spec_data[$v['taname']][] = $v;
        }
        $this->assign('g_spec_data',$g_spec_data);
        // 获得旧数据
        $old_data = M('goods_list')->where("glid='{$glid}'")->find();
        $old_data['combline'] = explode(',',$old_data['combline']);
        // p($old_data);
        $this->assign('old_data',$old_data);
        $this->display();
    }
    /**
     *  删除货品列表
     */
    public function del_goods_list(){
        // p(Q('get.'));die;
        $glid = Q('get.glid',0,'intval');
        $gid = Q('get.gid',0,'intval');
        M('goods_list')->where("glid='{$glid}'")->delete();
        $this->success("删除成功",U('index',array('gid'=>$gid)),'0.5'); 
    }


}